﻿using System.ComponentModel;
using System.Windows.Forms;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.Application
{
	public partial class AccountSetupPage : Form
	{
		public AccountSetupPage()
		{
			this.InitializeComponent();

			this.comboBoxAccountTypes.DataSource = this.StatementSourceTemplates;
			this.comboBoxAccountTypes.DisplayMember = "DisplayName";
		}

		private StatementSourceTemplate[] statementSourceTemplates;
		private StatementSource configuredStatementSource;

		public StatementSource ConfiguredStatementSource
		{
			get
			{
				return configuredStatementSource;
			}
			set { configuredStatementSource = value; }
		}

		[EditorBrowsable(EditorBrowsableState.Always),
		 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true)]
		public StatementSourceTemplate[] StatementSourceTemplates
		{
			get { return this.statementSourceTemplates; }
			set
			{
				if (value != this.statementSourceTemplates)
				{
					this.statementSourceTemplates = value;
					this.comboBoxAccountTypes.DataSource = value;
				}
			}
		}

		private void comboBoxAccountTypes_SelectedValueChanged(object sender, System.EventArgs e)
		{
			var template = (StatementSourceTemplate) this.comboBoxAccountTypes.SelectedValue;
			Control accountSetupPanel = (template != null && !string.IsNullOrEmpty(template.AccountSetupPanelId) ? SingletonContainer.Resolve<Control>(template.AccountSetupPanelId) : null);

			this.tableLayoutPanel.SuspendLayout();

			try
			{
				Control previousControl = this.tableLayoutPanel.GetControlFromPosition(0, 3);
				if (previousControl != null)
				{
					this.tableLayoutPanel.Controls.Remove(previousControl);
					this.tableLayoutPanel.RowStyles[3] = new RowStyle(SizeType.AutoSize);
				}

				labelConfigureAccount.Visible = (accountSetupPanel != null);
				if (accountSetupPanel != null)
				{
					this.tableLayoutPanel.Controls.Add(accountSetupPanel, 0, 3);
					accountSetupPanel.Dock = DockStyle.Fill;
					this.tableLayoutPanel.RowStyles[3] = new RowStyle(SizeType.Absolute, accountSetupPanel.Height + accountSetupPanel.Margin.Vertical);
				}
			}
			finally
			{
				this.tableLayoutPanel.ResumeLayout();
			}

			updateStatementSource();
		}

		private void updateStatementSource()
		{
			if (configuredStatementSource == null)
			{
				var template = (StatementSourceTemplate)this.comboBoxAccountTypes.SelectedValue;

				if (template != null)
				{
					configuredStatementSource = new StatementSource(template.Id);
				}
			}

			if (configuredStatementSource != null)
			{
				var parametersProvider = this.tableLayoutPanel.GetControlFromPosition(0, 3) as IStatementSourceParametersProvider;

				if (parametersProvider != null)
				{
					configuredStatementSource.Parameters = parametersProvider.GetParameters();
				}
			}
			else
			{
				configuredStatementSource.Parameters = null;
			}
		}

		private void AccountSetupForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			if ((e.CloseReason == CloseReason.UserClosing || e.CloseReason == CloseReason.None) &&
				this.DialogResult == System.Windows.Forms.DialogResult.OK)
			{
				updateStatementSource();
			}
		}
	}
}